package com.example.itemtouchhelperdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.SimpleItemAnimator;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.itemtouchhelperdemo.databinding.ActivityMainBinding;
import com.example.itemtouchhelperdemo.util.ItemTouchHelperCallBack;

import java.lang.reflect.Field;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements AvatarAdapter.OnItemListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    private AvatarAdapter avatarAdapter;
    private boolean canScroll = true;
    private ItemTouchHelper itemTouchHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        ConstraintLayout root = activityMainBinding.getRoot();
        setContentView(root);
        ArrayList<Integer> avatars = new ArrayList<>();
        ArrayList<Integer> originAvatars = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            int resId = getResources().getIdentifier("icon_" + i, "drawable", getPackageName());
            avatars.add(resId);
            avatars.add(resId);
            originAvatars.add(resId);
            originAvatars.add(resId);
        }

        avatarAdapter = new AvatarAdapter(avatars, this);
        activityMainBinding.avatarRv.setAdapter(avatarAdapter);
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 5, GridLayoutManager.VERTICAL, false);
        activityMainBinding.avatarRv.setLayoutManager(mGridLayoutManager);
        ((SimpleItemAnimator) activityMainBinding.avatarRv.getItemAnimator()).setSupportsChangeAnimations(false);
        ItemTouchHelperCallBack itemTouchHelperCallBack = new ItemTouchHelperCallBack(avatars, originAvatars, avatarAdapter);
        itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallBack);
        itemTouchHelper.attachToRecyclerView(activityMainBinding.avatarRv);
        itemTouchHelperCallBack.setDragListener(new ItemTouchHelperCallBack.DragListener() {
            @Override
            public void deleteState(boolean delete) {
                activityMainBinding.avatarDeleteCb.setChecked(delete);
                activityMainBinding.avatarDeleteContainer.setEnabled(delete);
            }

            @Override
            public void dragState(boolean start) {
                Log.e(TAG, "dragState: " + start);
                canScroll = !start;
                if (start) {
                    activityMainBinding.avatarDeleteContainer.setVisibility(View.VISIBLE);
                    activityMainBinding.avatarDeleteCb.setVisibility(View.VISIBLE);
                    activityMainBinding.avatarDeletePrompt.setVisibility(View.VISIBLE);
                } else {
                    activityMainBinding.avatarDeleteContainer.setVisibility(View.INVISIBLE);
                    activityMainBinding.avatarDeleteCb.setVisibility(View.INVISIBLE);
                    activityMainBinding.avatarDeletePrompt.setVisibility(View.INVISIBLE);
                    avatarAdapter.resetLongClickIndex(true);
                }
            }

            @Override
            public void clearView() {

            }

            @Override
            public void deleteView(int position) {

            }
        });

        try {
            Field scrollRunnableFiled = itemTouchHelper.getClass().getDeclaredField("mScrollRunnable");
            scrollRunnableFiled.setAccessible(true);
            Runnable scrollRunnable = (Runnable) scrollRunnableFiled.get(itemTouchHelper);
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    if (canScroll) {
                        scrollRunnable.run();
                    }
                }
            };
            scrollRunnableFiled.set(itemTouchHelper, runnable);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onLongClick(View v, AvatarAdapter.AvatarViewHolder holder, int position) {
        itemTouchHelper.startDrag(holder);
    }

    @Override
    public void onClickAvatar(int pos) {

    }
}